package com.yjz.aiagent.rag;

import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 基于内存 向量数据库的配置
 * @author YJZ
 */
@Configuration
public class LoveAppVectorStoreConfig {

    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;

    @Resource
    private MyTokenTextSplitter  myTokenTextSplitter;


    @Resource
    private MyKeywordEnricher myKeywordEnricher;


    @Bean // loveAppVectorStore 初始化bean 的名字
    VectorStore loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel) {
         // 初始化基于内存的向量数据库
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel)
                .build();
        // 加载文档
        List<Document> documents = loveAppDocumentLoader.loadMarkdowns();
       /* // 自主切分文档
        List<Document> splitDocument = myTokenTextSplitter.splitCustomized(documents);*/

        // 使用 基于AI 的元信息增强器（为文档补充元信息） 自动补充元信息
        List<Document> enrichDocuments = myKeywordEnricher.enrichDocument(documents);
        simpleVectorStore.add(enrichDocuments);
        return simpleVectorStore;
    }
}
